package the.lb.workbench.hlk.controller;

import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.context.UserContext;
import the.lb.workbench.hlk.dto.CourierCondition;
import the.lb.workbench.hlk.model.Courier;
import the.lb.workbench.hlk.service.ICourierService;
import the.lb.workbench.hlk.vo.CourierVO;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hlk/courier")
public class CourierController {

    @Resource
    private ICourierService courierService;

    @PostMapping("/add")
    public APIResult add(
            String courierName,
            String phone
    ) {
        Courier courier = new Courier();
        courier.setCourierName(courierName);
        courier.setPhone(phone);
        courier.setCreatedId(UserContext.userId());
        courier.setCreatedName(UserContext.userName());
        courier.setCreatedTime(new Date());
        courierService.save(courier);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids") List<String> ids) {
        courierService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
            @RequestParam(value = "courierId", required = true) String courierId,
            @RequestParam(value = "courierName", required = true) String courierName,
            @RequestParam(value = "phone", required = true) String phone
    ) {
        Courier courier = new Courier();
        courier.setCourierId(courierId);
        courier.setCourierName(courierName);
        courier.setPhone(phone);
        courier.setModifiedId(UserContext.userId());
        courier.setModifiedName(UserContext.userName());
        courier.setModifiedTime(new Date());
        courierService.updateById(courier);
        return APIResult.success();
    }

    @GetMapping("/{courierId}")
    public APIResult get(@PathVariable(value = "courierId") String courierId) {
        return APIResult.success(courierService.getById(courierId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<CourierVO>> page(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize
    ) {
        CourierCondition condition = new CourierCondition();
        condition.setKeyword(keyword);
        return APIResult.success(courierService.pageList(condition, pageNum, pageSize));
    }
}
